using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using CHEnhanceEfCore.Common;
using CHEnhanceEfCore.Common.Page;
using CHEnhanceEfCore.Config.Log;
using CHEnhanceEfCore.Models;
using CHEnhanceEfCore.Repository;

namespace CHEnhanceEfCore.Service.impl
{
    /// <summary>
    /// author: chenhao
    /// </summary>
    public class UserTaskServiceImpl : IUserTaskService
    {
        private readonly UserTaskRepository _userTaskRepository;
        private readonly LogUtils _logUtils;

        public UserTaskServiceImpl(UserTaskRepository userTaskRepository, LogUtils logUtils)
        {
            _userTaskRepository = userTaskRepository;
            _logUtils = logUtils;
        }

        public async Task<List<UserTask>> getUserTaskListAsync()
        {
            return await _userTaskRepository.selectUserTaskListAsync();
        }

        public async Task<UserTask> addAsync(UserTask userTask)
        {
            //获取主键id
            Guid userTaskId = userTask.id;
            UserTask oldUserTask = await _userTaskRepository.selectByIdAsync(userTaskId);
            if (null == oldUserTask)
            {
                return await _userTaskRepository.saveAsync(userTask);
            }

            return null;
        }

        public Task<Guid> generateUserTaskAsync()
        {
            Guid newGuid = Guid.NewGuid();
            _logUtils.Info($"generateUserTaskAsync===> id: {newGuid}");
            return Task.FromResult(newGuid);
        }

        public async Task<bool> deletedAsync(List<Guid> userTaskIdList)
        {
            if (null == userTaskIdList || userTaskIdList.Count == 0)
            {
                return false;
            }

            List<UserTask> userTaskList = await _userTaskRepository.selectUserTaskByIds(userTaskIdList);
            if (null == userTaskList || userTaskList.Count == 0)
            {
                return false;
            }

            return await _userTaskRepository.deletedRangeAsync(userTaskList);
        }
    }
}